# -*- coding:utf-8 -*-
"""
Created on Jun 6, 2011

@author: Lithuanian
"""
from bond import Bond
from loa_example import AlphaBetaAgent
from game_runner import GameRunner
from loa_game import WHITE, BLACK, LinesOfActionState
from experimentClass.TestTimebond import TestTimeBond
class Tester():
    
    def timeTest(self):
        Wagent = TestTimeBond()
        Bagent = AlphaBetaAgent()
        agents = {}
        agents[WHITE] = Wagent
        agents[BLACK] = Bagent
#        exp = Experiment(Wagent, "Bond" ,Bagent, "alphaBeta", 20, False)
        sizes = xrange(4,13)
        setup   = []
        update  = []
        turn    = []
        for N in sizes:
            state = LinesOfActionState(N, 50)
            print "Starting game..."
            gr = GameRunner(state, agents, 20, 20)
            winner = self.runGame(gr)
            setup.append(gr.setupTime)
            update.append(gr.updateTime)
            turn.append(gr.turnTimes)
            self.outputTimeTestResults(gr.setupTime, gr.updateTime, gr.turnTimes, N)
            
    def outputTimeTestResults(self, setups, updates, turns, size):
        setupRes    = open("C:\Documents and Settings\Boris\Desktop\AIresults\\setuptimes.txt","a")
        updateRes   = open("C:\Documents and Settings\Boris\Desktop\AIresults\\updateTimes.txt","a")
        setupRes.writelines("size\tturn\tsetuptime\tratio\n")
        updateRes.writelines("size\tturn\tupdatetime\tratio\n")

        #calc avg turn...
        avgTurnTime = 0
        for i in turns:
            avgTurnTime += i 
        avgTurnTime = float(avgTurnTime)/len(turns)
        #calc updates stuff...
        avgUpdateTime = 0
        for i in updates:
            avgUpdateTime += i
        avgUpdateTime = float(avgUpdateTime)/len(updates)
        #calc setup stuff....
        avgSetupTime = 0
        for i in updates:
            avgSetupTime += i
        avgSetupTime = float(avgSetupTime)/len(updates)
        
        #write stuff                
        sratio = 100*avgSetupTime/avgTurnTime
        uratio = 100*avgUpdateTime/avgTurnTime
        setupRes.writelines(str(size)+"\t"+str(avgTurnTime)+"\t"+str(avgSetupTime)+"\t"+str(sratio)+"\n")
        updateRes.writelines(str(size)+"\t"+str(avgTurnTime)+"\t"+str(avgUpdateTime)+"\t"+str(uratio)+"\n")
        setupRes.close()
        updateRes.close()
        
    
    def initAgentTime(self):
        Wagent = Bond()
        Bagent = AlphaBetaAgent()
        time = [10,20,30,40,50]
        exp = Experiment(Wagent, "Bond" ,Bagent, "alphaBeta", time, True)
        return exp
        
    def agentTime(self):
        exp = self.initAgentTime()
        agents = {}
        agents[WHITE] = exp.white
        agents[BLACK] = exp.black
        if exp.isTimeList:
            if exp.isTurnTimeTest:
                for time in exp.turnTime:
                    gr = GameRunner(None, agents, time, exp.setupTime)
            if exp.isSetupTimeTest:
                for setup in exp.setupTime:
                    gr = GameRunner(None, agents, exp.turnTime, setup)
        else:
            gr = GameRunner(None, agents, exp.turnTime, exp.setupTime)
            self.runGame(gr)
            
    def runGame(self, gr):
        '''
        @param gr: game runner object 
        '''
        winner = gr.run()
        
class Experiment():
    def __init__(self, Wagent, whiteName, Bagent, blackName, turnTime, setupTime, \
                 isTurnTimeTest, isSetupTimeTest):
        self.white              = Wagent
        self.whiteName          = whiteName
        self.black              = Bagent
        self.blackName          = blackName
        self.turnTime           = turnTime
        self.setupTime          = setupTime
        self.isTurnTimeTest     = isTurnTimeTest
        self.isSetupTimeTest    = isSetupTimeTest


#agents = {}
#agents[WHITE] = TestTimeBond()
#agents[BLACK] = AlphaBetaAgent()
#
#state = LinesOfActionState(7, 50)
#
#print "Starting Game...."
#winner = GameRunner(state, agents, 5, 1).run()

t = Tester()
t.timeTest()